<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

/**
 * App\Models\Node
 *
 * @property int $id
 * @property string $name 节点ID
 * @property string $router_name 路由别名,权限认证标识  admin.user.index
 * @property int $pid 父ID
 * @property string $is_menu 是否为菜单:0否 1是
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $deleted_at
 * @method static \Illuminate\Database\Eloquent\Builder|Node newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Node newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Node query()
 * @method static \Illuminate\Database\Eloquent\Builder|Node whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Node whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Node whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Node whereIsMenu($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Node whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Node wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Node whereRouterName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Node whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Node extends Base
{
    // 访问器 getAttribute  menu  Menu
    // 访问器 中间的名字可自定义 不需要是数据库里的字段名  后面引用的时候对应名称即可
    // 需数组调用
    public function getMenuAttribute()
    {
        $menu = $this->attributes['is_menu'];
        if ($menu == '0') {
            return "<span class=\"label label-danger radius\">否</span>";
        }
        return "<span class=\"label label-success radius\">是</span>";
    }

    // 获取全部数据
    public function getAllList()
    {
        $data = self::get()->toArray();
        return $this->treeLevel($data);
    }

    // 获取层级数据
    public function getTreeList()
    {
        $res   = session('admin_nodes');
        $query = Node::where('is_menu', '1');
        if (is_array($res)) {
            $query->whereIn('id', array_values($res));
        }
        $data = $query->get()->toArray();
        return $this->subTree($data);

        /**
        if ($res === true) {
            $data = self::where('is_menu', '1')->get()->toArray();
            return $this->subTree($data);
        }
        $nodes = array_values(session('admin_nodes'));
        $data  = self::where('is_menu', '1')
            ->whereIn('id', $nodes)->get()->toArray();
        return $this->subTree($data);
        */
    }

    // 无条件层级结构(存父子关系树)-- --较上一个方法少了查询条件
    public function getTreeAll()
    {
        $res = session('admin_nodes');
        if ($res === true) {
            $data = self::get()->toArray();
            return $this->subTree($data);
        }
        $nodes = array_values(session('admin_nodes'));
        // dd($nodes);
        $data = self::whereIn('id', $nodes)->get()->toArray();
        return $this->subTree($data);
    }
}
